//
//  H_BingDVC.m
//  Honey
//
//  Created by 贾亚涵 on 16/5/8.
//  Copyright © 2016年 贾亚涵. All rights reserved.
//

#import "H_BingDVC.h"

@interface H_BingDVC ()<UITextFieldDelegate>
{
    UILabel * _backLabel;
    UILabel * _navBarLabel;
    
    UILabel * _bingDTipUIL1;
    UILabel * _bingDTipUIL2;
    UITextField * _bingDTF;
    UIButton * _bingDBtn;
    NSString *_LPhoneNumStr;
}

@end

@implementation H_BingDVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self NavBarBtn];
    
    [self creatBingDView];
}

-(void)NavBarBtn
{
    UIImage * leftBtnImage = [[UIImage imageNamed:@"tl_photo_slide_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftBtnImage style:UIBarButtonItemStylePlain target:self action:@selector(leftBerBtnClicked:)];
    _backLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 60, 40)];
    _backLabel.text = @"返回";
    _backLabel.textColor = [UIColor whiteColor];
    [self.navigationController.navigationBar addSubview:_backLabel];
    
    _navBarLabel = [[UILabel alloc] initWithFrame:CGRectMake(Screen_Width/2-40, 6, 80, 30)];
    _navBarLabel.text = @"匹配账号";
    _navBarLabel.textColor = [UIColor whiteColor];
    _navBarLabel.textAlignment = NSTextAlignmentCenter;
    [self.navigationController.navigationBar addSubview:_navBarLabel];
}

-(void)leftBerBtnClicked:(id)sender
{
    [self bingDPackKeyboard];
    //返回主界面
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
#pragma mark- 收键盘
-(void)bingDPackKeyboard
{
    [_bingDTF resignFirstResponder];
}

-(void)creatBingDView
{

    

    UIImageView * _bingDLeftIV = [[UIImageView alloc] initWithFrame:CGRectMake(17, 144, 55, 53)];
    _bingDLeftIV.image = [UIImage imageNamed:@"BingD_L"];
    [self.view addSubview:_bingDLeftIV];
    UIImageView * _bingDRightIV = [[UIImageView alloc] initWithFrame:CGRectMake(Screen_Width-72, 144, 55, 53)];
    _bingDRightIV.image = [UIImage imageNamed:@"BingD_R"];
    [self.view addSubview:_bingDRightIV];

    _bingDTipUIL1 = [[UILabel alloc] initWithFrame:CGRectMake(Screen_Width/2-100, 144, 200, 30)];
    _bingDTipUIL1.text = @"亲，蜜糖是两个人玩的应用";
    _bingDTipUIL1.textColor = [UIColor lightGrayColor];
    _bingDTipUIL1.textAlignment = NSTextAlignmentCenter;
    _bingDTipUIL1.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:_bingDTipUIL1];
    _bingDTipUIL2 = [[UILabel alloc] initWithFrame:CGRectMake(Screen_Width/2-120, CGRectGetMaxY(_bingDTipUIL1.frame)-7, 240, 30)];
    _bingDTipUIL2.textAlignment = NSTextAlignmentCenter;
    _bingDTipUIL2.font = [UIFont systemFontOfSize:13];
    _bingDTipUIL2.textColor = [UIColor lightGrayColor];
    _bingDTipUIL2.text = @"输入另一半的账号,来匹配开通蜜糖哟";
    [self.view addSubview:_bingDTipUIL2];

    _bingDTF = [[UITextField alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(_bingDTipUIL2.frame)+20, Screen_Width-10, 40)];
    _bingDTF.placeholder = @"请输入另一半的手机号";
    _bingDTF.textColor = [UIColor orangeColor];
    _bingDTF.borderStyle = UITextBorderStyleRoundedRect;
    _bingDTF.textAlignment = NSTextAlignmentCenter;
    _bingDTF.delegate = self;
    _bingDTF.backgroundColor = [UIColor whiteColor];
    _bingDTF.keyboardType = UIKeyboardTypeNumberPad;
    [_bingDTF addTarget:self action:@selector(BingDTFChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_bingDTF];

    _bingDBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_bingDTF.frame)+30, Screen_Width-20, 30)];
    _bingDBtn.backgroundColor = [UIColor colorWithRed:246/255.0 green:157/255.0 blue:182/255.0 alpha:1];
    [_bingDBtn setTitle:@"绑定登录" forState:UIControlStateNormal];
    _bingDBtn.layer.cornerRadius = 8.0f;
    [_bingDBtn addTarget:self action:@selector(BingDBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_bingDBtn];
}

//对注册信息的限制
-(void)BingDTFChanged:(UITextField *)sender
{
    NSCharacterSet * CS = [[NSCharacterSet characterSetWithCharactersInString:PhoneCanInput] invertedSet];
    NSString * str = [[_bingDTF.text componentsSeparatedByCharactersInSet:CS] componentsJoinedByString:@""];
    
    if (_bingDTF.text.length <= 11 && [str isEqualToString:_bingDTF.text])
    {
        _LPhoneNumStr = _bingDTF.text;
        
    }else
    {
        _bingDTF.text = _LPhoneNumStr;
    }

}
#pragma UITextFieldDelegate
//点击界面view 触发  收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_bingDTF resignFirstResponder];
}

-(void)BingDBtnClicked:(id)sender
{
    if (sender && [sender isKindOfClass:[UIButton class]])
    {
        [self bingDPackKeyboard];
        //调用提示框第三方
        MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.color = [UIColor orangeColor];
        if (_bingDTF.text != nil)
        {
            //判断手机号的长度
            if (_bingDTF.text.length == 11)
            {
                if ([@"13,15,17,18" containsString:[_bingDTF.text substringToIndex:2]])
                {
                    [hud hide:YES];
                    [self checkBingDinfoFromMyData];
                }else
                {
                    [hud setMode:MBProgressHUDModeText];
                    [hud setLabelText:@"请输入正确的手机号"];
                    [hud hide:YES afterDelay:1];
                }

            }else
            {
                [hud setMode:MBProgressHUDModeText];
                [hud setLabelText:@"请输入11位手机号"];
                [hud hide:YES afterDelay:1];

            }
        }else
        {
            [hud setMode:MBProgressHUDModeText];
            [hud setLabelText:@"手机号为空，请输入手机号"];
            [hud hide:YES afterDelay:1];
        }
        

    }
}
//校对绑定的信息是否已经注册
-(void)checkBingDinfoFromMyData
{
    H_CoreDataDAL * dal2 = [H_CoreDataDAL new];
    //调用第三方提示框
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.color = [UIColor orangeColor];
    if (![dal2 checkBingDInfoPhoneNum:_bingDTF.text])
    {
        [hud setMode:MBProgressHUDModeText];
        [hud setLabelText:@"绑定的用户账号不存在"];
        [hud hide:YES afterDelay:1.5];
    }else
    {
        if ([_bingDTF.text isEqualToString:_phoneNum]) {
            [hud setMode:MBProgressHUDModeText];
            [hud setLabelText:@"不能与自己绑定呦"];
            [hud hide:YES afterDelay:1.5];
        }
        else{
            
            if ([dal2 checkIsBingDPhoneNum:_bingDTF.text] != 3) {
                [hud setMode:MBProgressHUDModeText];
                [hud setLabelText:@"该手机号已经绑定或正在进行绑定"];
                [hud hide:YES afterDelay:1.5];
            }
            else{
                [self bingDSuccess];
                [hud setMode:MBProgressHUDModeText];
                [hud setLabelText:@"已经发送绑定申请"];
                [hud hide:YES afterDelay:1];
            }
            
        }
        
    }
}

//绑定信息存入数据库
-(void)bingDSuccess
{
    H_CoreDataDAL * dal = [H_CoreDataDAL new];
    [dal applyForPhone:_phoneNum bdphone:_bingDTF.text];
    
}

@end
